import { ServiceProto } from "../../../proto/ServiceProto";
import { createServer } from 'http'
import { WsConnection, WsServer } from "mrhqq-ws";
import Koa from 'koa'
export class NetWebSocket {
    constructor(app: Koa) {
        let ws = new WsServer(ServiceProto.getMap(), {
            port: 8080,
            logger: console,
            jsonEnabled: false,
        });
        ws.start(createServer(app.callback()))
            .then(() => {
                console.log("启动成功")
            });
        ws.flows.preRecvDataFlow.push(v => {
            let conn = v.conn as WsConnection;
            let result = v.result;
            this.handMsg(conn, result)
            return v;
        })

        ws.flows.postDisconnectFlow.push(v => {
            let conn = v.conn as WsConnection;
            console.log(conn.id + " is leave")
            return v;
        })
    }
    handMsg(con: WsConnection, msg: any) {
        let msgName = msg.msgName;
        let buffer = msg.buffer;
        // result: { msgName, buffer: buff }
    }
}


